
package com.safebox.security;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

import android.util.Log;



public class DataSecurity {
    public static final String TAG = "DataSecurity";
    private static final String DES_ALOGRITHM = "DES";   

    private static final String strDefaultKey = "national";  
    
    /**
     * 将byte数组转换为表示16进制值的字符串， 如：byte[]{8,18}转换为：0813， 
     * 和public static byte[] hexStr2ByteArr(String strIn) 互为可逆的转换过程
     *
     * @param arrB
     *            需要转换的byte数组
     * @return 转换后的字符串
     * @throws Exception
     *             本方法不处理任何异常，所有异常全部抛出
     */
    public static String byteArr2HexStr(byte[] arrB) throws Exception {
     int iLen = arrB.length;
     // 每个byte用两个字符才能表示，所以字符串的长度是数组长度的两倍
     StringBuffer sb = new StringBuffer(iLen * 2);
     for (int i = 0; i < iLen; i++) {
      int intTmp = arrB[i];
      // 把负数转换为正数
      while (intTmp < 0) {
       intTmp = intTmp + 256;
      }
      // 小于0F的数需要在前面补0
      if (intTmp < 16) {
       sb.append("0");
      }
      sb.append(Integer.toString(intTmp, 16));
     }
     return sb.toString();
    }
    
    /**
     * 将表示16进制值的字符串转换为byte数组， 和public static String byteArr2HexStr(byte[] arrB)
     * 互为可逆的转换过程
     *
     * @param strIn
     *            需要转换的字符串
     * @return 转换后的byte数组
     * @throws Exception
     *             本方法不处理任何异常，所有异常全部抛出
     * @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
     */
    public static byte[] hexStr2ByteArr(String strIn) throws Exception {
     byte[] arrB = strIn.getBytes();
     int iLen = arrB.length;
    
     // 两个字符表示一个字节，所以字节数组长度是字符串长度除以2
     byte[] arrOut = new byte[iLen / 2];
     for (int i = 0; i < iLen; i = i + 2) {
      String strTmp = new String(arrB, i, 2);
      arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
     }
     return arrOut;
    }
    
   
    /**
     * encode
     */
    private static byte[] encode(byte[] arrB, byte[] key, String alg) throws Exception {
        Key deskey;
        if (key != null)
            deskey = getKey(arrB, alg);
        else
            deskey = getKey(strDefaultKey.getBytes(), alg);
        Cipher encryptCipher = Cipher.getInstance(alg);
        encryptCipher.init(Cipher.ENCRYPT_MODE, deskey);
        return encryptCipher.doFinal(arrB);
    }
    
    /**
     * encrypt using des
     */
    public static String desEncrypt(String strIn){
        String rt = null;
        try {
            rt = byteArr2HexStr(encode(strIn.getBytes(), null, DES_ALOGRITHM));
        } catch (Exception ex) {
            Log.e(TAG, "DataSecurity ERROR:" + ex.toString());
        }
        return rt;
    }
    
    //reserved
    private static String encrypt(String strIn, String key, String alogrithm){
        String rt = null;
        try {
            rt = byteArr2HexStr(encode(strIn.getBytes(), key.getBytes(), alogrithm));
        } catch (Exception ex) {
            Log.e(TAG, "DataSecurity ERROR:" + ex.toString());
        }
        return rt;
    }
    
    /**
     * decode
     */
    private static byte[] decode(byte[] arrB, byte[] key, String alg) throws Exception {
        Key deskey;
        if (key != null)
            deskey = getKey(arrB, alg);
        else
            deskey = getKey(strDefaultKey.getBytes(), alg);
        Cipher decryptCipher = Cipher.getInstance(alg);
        decryptCipher.init(Cipher.DECRYPT_MODE, deskey);
        return decryptCipher.doFinal(arrB);

    }
    
    /**
     * decrypt string
     */
    public static String desDecrypt(String strIn){
        String rt = null;
        try {
            rt = new String(decode(hexStr2ByteArr(strIn), null, DES_ALOGRITHM));
        } catch (Exception ex) {
            Log.e(TAG, "DataSecurity ERROR:" + ex.toString());
        }
     return rt;
    }
    
    //reserved. not use
    private static String decrypt(String strIn, String key, String alogrithm) {
        String rt = null;
        try {
            rt = new String(decode(hexStr2ByteArr(strIn), key.getBytes(), alogrithm));
        } catch (Exception ex) {
            Log.e(TAG, "DataSecurity ERROR:" + ex.toString());
        }
        return rt;
        
       }
    
    //md5
    private static byte[] md5_encode(byte[] arrB) throws Exception {
        java.security.MessageDigest alg = java.security.MessageDigest.getInstance("MD5");
        alg.update(arrB);
        return alg.digest();      
    }
    
    public static String md5(String strIn) {
        String rt = null;
        try {
            rt = byteArr2HexStr(md5_encode(strIn.getBytes()));
        } catch (Exception e) {
            Log.e(TAG, "DataSecurity ERROR:" + e.toString());
        }
        return rt;
    }
    /**
     * generate 8-bit key
     */
    private static Key getKey(byte[] arrBTmp, String alg) throws Exception {
    
     byte[] arrB = new byte[8];
    
    
     for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
      arrB[i] = arrBTmp[i];
     }
    
     
     Key key = new javax.crypto.spec.SecretKeySpec(arrB, alg);
    
     return key;
    }
    



}
